/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AdminCenterMain;

import AnalysistInterface.IAnalysist;
import CommonValue.SystemSign;
import CommonValue.SystemTimer;
import DTO.ControlDTO;
import MyLogging.ILog;
import SocketBusiness.ServerListen;
import DTO.PacketDTO;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ReceiveReportFromWS extends ServerListen {

    private HashMap<String, IAnalysist> mapIA = new HashMap<String, IAnalysist>();
    private PacketDTO _hourlyReport = new PacketDTO(SystemSign.AdminCenter);

    public ReceiveReportFromWS(String systemSign, int port, ControlDTO operation, PacketDTO _hourlyReport) {
        super(systemSign, port, operation);
        this._hourlyReport = _hourlyReport;
    }

    @Override
    public void business() {
        boolean finish = false;
        PacketDTO temp = new PacketDTO(this.getSystemSign());
        do {
            try {
                Thread.sleep(SystemTimer.CheckReportSleep);
            } catch (InterruptedException ex) {
                Logger.getLogger(ReceiveReportFromWS.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (!this.getOperation().getFlag()) {
                System.out.println(this.getSystemSign() + " DA DUNG HOAT DONG <<<<<<<<<<<<<<<<");
                continue;
            }
            Object obj = new Object();
            try {
                obj = this.receiveMessage();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ReceiveReportFromWS.class.getName()).log(Level.SEVERE, null, ex);
            }
                
            if (obj == null) {
                System.out.println("packetDTO is null");
                continue;
            }
            if (!obj.getClass().equals(PacketDTO.class)) {
                System.out.println("Invalid Type Class");
                continue;
            }
            ILog.writeLog(this.getClass() + "Da nhan duoc packet", this.getSystemSign());
            synchronized (this._hourlyReport) {
                //_hourlyReport.clearBuffer();
                _hourlyReport.addAllToBuffer(((PacketDTO) obj).getBuffer());
                System.out.println("so khoi du lieu trong packet __hourlyReport la :  " + this._hourlyReport.getBuffer().size());
            }

            
        } while (!finish);
    }
}
